/*
 *  Copyright (C) 2010:
 *                 Buzzanca Marco <marco.bzn@gmail.com>
 *         Monaco Davide Giuseppe <black.ralkass@gmail.com>
 *              Petralia Veronica <veronicapetralia@gmail.com>
 *
 * This file is part of DS2011BMP
 *
 * DS2011BMP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DS2011BMP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/**
 * @file wfqueuemanager.cpp
 * @author Davide Giuseppe Monaco
 *
 * This file contains WFQueueManager class implementation.
 */

#include "wfqueuemanager.h"

WFQueueManager::WFQueueManager(void) { }

WFQueueManager::WFQueueManager(const Port& port)
  : _server(port) { }

WFQueueManager::WFQueueManager(const Address& addr)
  : _server(addr) { }

WFQueueManager::~WFQueueManager(void) { }

boost::uint16_t WFQueueManager::getPort(void) const
{ return _server.getPort(); }

void WFQueueManager::setPort(boost::uint16_t port)
{ _server.setPort(port); }

void WFQueueManager::setPort(const Address& address)
{ _server.setPort(address.getPort()); }

int WFQueueManager::enqueue(const Workflow& wf)
{
  //  boost::mutex::scoped_lock lock(_m);
  _wfQueue.push(wf);
  return 0;
}

Workflow WFQueueManager::dequeue(void)
{
    //boost::mutex::scoped_lock lock(_m);
  return _wfQueue.pop();
}

void WFQueueManager::start(void)
{
  _s_thread = boost::shared_ptr<boost::thread>(
      new boost::thread(
        boost::bind(&WFQueueManager::run, this)));

  _s_thread->join();
}

void WFQueueManager::run(void)
{
  _server.bind();

  std::cout << "WFQueueManager ready..." << std::endl;

  for (;;) {

    char recv_buf[UDPReceiver::MAX_BUF_LEN];
    size_t recv_len = _server.receive(recv_buf, UDPReceiver::MAX_BUF_LEN);

    buffer_pointer buffer = buffer_pointer(new char[recv_len]);
    memcpy(buffer.get(), recv_buf, recv_len);

    boost::shared_ptr<boost::thread>(
        new boost::thread(
          boost::bind(&WFQueueManager::serve, this, buffer)));
  }
}

void WFQueueManager::serve(buffer_pointer buf)
{
  // stringstream needed for deserialization
  std::stringstream ss_ (std::stringstream::in | std::stringstream::out);

  // create a string, then put it in the stringstream
  std::string received_ (buf.get());
  ss_ << received_;

  // the destination workflow
  Workflow wf_;

  // text_iarchive bound to stringstream
  boost::archive::text_iarchive ia_ (ss_);

  // unserialize, producing a Workflow
  ia_ >> wf_;

  // DEBUG
  //std::cout << "Task 0: id: " << wf_.getTask(0).getID() << std::endl;
  //std::cout << "Workflow size: " << wf_.size() << std::endl;

  // enqueue
  this->enqueue(wf_);
}
